package com.example.demo.controller;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 自定义错误处理控制器
 * 提供友好的错误页面和API错误响应
 * 
 * @author Spring AI Demo
 */
@Controller
public class CustomErrorController implements ErrorController {

    /**
     * 处理错误请求
     * 
     * @param request HTTP请求
     * @return 错误响应
     */
    @RequestMapping("/error")
    @ResponseBody
    public Map<String, Object> handleError(HttpServletRequest request) {
        Map<String, Object> errorResponse = new HashMap<>();
        
        // 获取错误状态码
        Integer statusCode = (Integer) request.getAttribute("jakarta.servlet.error.status_code");
        String requestUri = (String) request.getAttribute("jakarta.servlet.error.request_uri");
        
        if (statusCode == null) {
            statusCode = HttpStatus.INTERNAL_SERVER_ERROR.value();
        }
        
        errorResponse.put("status", statusCode);
        errorResponse.put("error", getErrorMessage(statusCode));
        errorResponse.put("path", requestUri);
        errorResponse.put("timestamp", System.currentTimeMillis());
        
        // 根据错误类型提供帮助信息
        switch (statusCode) {
            case 404:
                errorResponse.put("message", "页面未找到");
                errorResponse.put("suggestions", new String[]{
                    "访问主页: http://localhost:8080",
                    "查看API文档: http://localhost:8080/api/ai/features",
                    "健康检查: http://localhost:8080/actuator/health"
                });
                break;
            case 500:
                errorResponse.put("message", "服务器内部错误");
                errorResponse.put("suggestions", new String[]{
                    "检查应用日志",
                    "确认API密钥配置正确",
                    "重启应用服务"
                });
                break;
            default:
                errorResponse.put("message", "请求处理失败");
                errorResponse.put("suggestions", new String[]{
                    "检查请求格式",
                    "查看API文档",
                    "联系技术支持"
                });
        }
        
        return errorResponse;
    }
    
    /**
     * 根据状态码获取错误消息
     * 
     * @param statusCode HTTP状态码
     * @return 错误消息
     */
    private String getErrorMessage(int statusCode) {
        switch (statusCode) {
            case 400:
                return "Bad Request";
            case 401:
                return "Unauthorized";
            case 403:
                return "Forbidden";
            case 404:
                return "Not Found";
            case 500:
                return "Internal Server Error";
            case 502:
                return "Bad Gateway";
            case 503:
                return "Service Unavailable";
            default:
                return "Unknown Error";
        }
    }
} 